package com.mlk.car.service.impl;

import com.mlk.car.mapper.EngCarEvilLogMapper;
import com.mlk.car.mapper.EngCarMapper;
import com.mlk.car.po.EngCar;
import com.mlk.car.po.EngCarEvilLog;
import com.mlk.car.service.EngCarService;
import com.mlk.car.vo.EngCarVO;
import com.mlk.core.component.page.Page;
import com.mlk.core.component.page.Pager;
import com.mlk.core.exception.GlobalException;
import com.mlk.core.util.ServerResponse;
import com.mlk.core.util.SessionUtils;
import com.mlk.engSystem.mapper.EngSysEmpMapper;
import com.mlk.engSystem.po.EngSysEmp;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class EngCarServiceImpl implements EngCarService {

    @Resource
    private EngCarMapper engCarMapper;


    @Resource
    private EngCarEvilLogMapper engCarEvilLogMapper;

    @Resource
    private EngSysEmpMapper engSysEmpMapper;

    /**
     * 分页查询数据
     * @param vo
     * @return
     */
    @Page
    @Override
    public Pager listPage(EngCarVO vo) {
        return Pager.adapt(this.engCarMapper.selectForPage(vo));
    }

    /**
     * 新增车辆信息
     * @param car
     * @return
     */
    @Override
    public ServerResponse add(EngCar car) {
         int count = this.engCarMapper.insert(car);
         if(count == 0){
             throw new GlobalException("操作失败");
         }
         return ServerResponse.ok("操作成功");
    }

    /**
     * 车辆分配
     * @param car
     * @return
     */
    @Override
    public ServerResponse allocate(EngCar car) {
        int count = this.engCarMapper.updateById(car);
        if(count == 0){
            throw new GlobalException("操作失败");
        }
        return ServerResponse.ok("操作成功");
    }

    /**
     * 更新车辆信息
     * @param car
     * @return
     */
    @Override
    public ServerResponse update(EngCar car) {
        int count = this.engCarMapper.updateById(car);
        if(count == 0){
            throw new GlobalException("操作失败");
        }
        return ServerResponse.ok("操作成功");
    }

    /**
     * 添加车辆违规记录
     * @param log
     * @return
     */
    @Override
    public ServerResponse addLog(EngCarEvilLog log) {
        log.setCreateTime(new Date());
        log.setCreateName(SessionUtils.getUser().getSysUser().getName());
        int count = this.engCarEvilLogMapper.insert(log);
        if(count == 0){
            throw new GlobalException("添加失败");
        }

        return ServerResponse.ok("添加成功");
    }

    @Override
    @Page
    public Pager listPageLog(Integer carId) {
        return Pager.adapt(this.engCarEvilLogMapper.listForPage(carId));
    }

    /**
     * 判断该用户是否绑定过车辆
     * @param openId
     * @return
     */
    @Override
    public ServerResponse checkOwnCar(String openId) {
        EngSysEmp emp = this.engSysEmpMapper.selectOne(EngSysEmp.builder().openId(openId).build());
        if(emp.getCarId() == null){
            return ServerResponse.fail("该用户没有绑定车辆");
        }

        return ServerResponse.ok("该用户已经绑定了车辆");
    }

}
